m = Map("firmware", translate("Firmware"))

update = m:section(TypedSection, "firmware", translate("Update Firmware"), translate("Version File URL is required and have to be URL prefixed with the'version'."))
update.anonymous = true

auto_check = update:option(ListValue, "auto_check", translate("Check Every"))
auto_check:value("0", translate("Disable"))
auto_check:value("* */12 * * *", translate("12 hours"))
auto_check:value("* * */1 * *", translate("day"))
auto_check:value("* * */3 * *", translate("3 days"))
auto_check:value("* * * * */1", translate("week"))
auto_check:value("* * * */0.5 *", translate("half month"))
auto_check:value("* * * */1 *", translate("month"))
auto_check:value("* * * */2 *", translate("2 months"))
auto_check:value("* * * */3 *", translate("3 months"))
auto_check:value("* * * */6 *", translate("half year"))
auto_check.default = "0"

version_url = update:option(Value, "version_url", translate("Verison URL"))
version_url.rmempty = false

download_to = update:option(Value, "download_to", translate("Download To"))
download_to:value("/tmp")
download_to.default = "/tmp"
download_to.datatype = "directory"
download_to.rmempty = false

check_for_update = update:option(Button, "check_for_update", translate(" "))
check_for_update.inputtitle = translate("Check For Update")
check_for_update.inputstyle = nil

current_version = luci.sys.exec("/etc/init.d/firmware echo_current_version")

if current_version:match('[0-9]+_[0-9+]') then
	check_for_update.description = translate("Local Version: ") .. current_version
	current_version_true = true
else
	check_for_update.description = translate("Local Version: Unknow")
end

function check_for_update.write(self, section, value)
	lastest_version = luci.sys.exec("/etc/init.d/firmware echo_lastest_version")
	if lastest_version:match('[0-9]+_[0-9+]') then
		if current_version_true == false then
			update_now.inputstyle = "apply"
		elseif lastest_version == current_version then
			update_now.inputtitle = "Lastest version installed"
		else
			lastest_version_true = (luci.sys.exec("/etc/init.d/firmware check_update_true %q %q" % {lastest_version,current_version}) == 0 )
			if lastest_version_true then
				update_now.inputstyle = "apply"
			end
			update_now.inputstyle = "apply"
		end
		update_now.description = "Server Version: " .. lastest_version
	else
		update_now.description = "Server Version: Unknow"
	end
	if update_now.inputstyle == "apply" then
		luci.sys.exec("/etc/init.d/firmware lastest_version_tmp %q" % lastest_version)
		update_now.inputtitle = "Update Now"
		update_now.description = "<span style='color:#47a800'>" .. update_now.description .. "</span>"
	end
end

update_now = update:option(Button, "update_now", translate(" "))
update_now.inputtitle = "..."
lastest_version = luci.sys.exec("/etc/init.d/firmware lastest_version_tmp_get")
if lastest_version:match('[0-9]+_[0-9+]') then
	update_now.inputstyle = "apply"
	update_now.inputtitle = "Update Now"
	update_now.description = "Server Version: " .. lastest_version
	update_now.description = "<span style='color:#47a800'>" .. update_now.description .. "</span>"
end
function update_now.write(self, section, value)
	if update_now.inputtitle ~= "..." then
		update_now.inputtitle = "Updating ..."
		update_now.description = "Please do not power off router while updating"
		update_now.description = luci.sys.exec("/etc/init.d/firmware update_now "..lastest_version)
		if update_now_return == "Firmware updated. Rebooting ..." then
			update_now.inputtitle = "Rebooting ..."
			update_now.description = "<span style='color:#47a800'>"..update_now.description.."</span>"
		else
			update_now.inputtitle = "Try Again"
			update_now.description = "<span style='color:#ff4800'>"..update_now.description.."</span>"
		end
	end
end

configuration = m:section(TypedSection, "firmware", translate("Restore Configuration"))
configuration.anonymous = true

configuration_location = configuration:option(Value, "configuration_location", translate("Location ( Path / URL )"))

configuration_wifi_ssid = configuration:option(Value, "configuration_wifi_ssid", translate("WiFi SSID"))

configuration_wifi_password = configuration:option(Value, "configuration_wifi_password", translate("WiFi Password"))
configuration_wifi_password.password = true
configuration_wifi_password.datatype = "minlength(8)"

configuration_restore = configuration:option(Button, "configuration_restore", translate(" "))
configuration_restore.inputstyle = "apply"
configuration_restore.inputtitle = "Restore"
function configuration_restore.write(self, section, value)
	configuration_restore.inputtitle = "Restoring ..."
	configuration_restore.description = luci.sys.exec("/etc/init.d/firmware configuration_restore")
	configuration_restore.inputtitle = "Restore"
	if configuration_restore.description == "Done." then
		configuration_restore.description = "<span style='color:#47a800'>"..configuration_restore.description.."</span>"
	else
		configuration_restore.description = "<span style='color:#ff4800'>"..configuration_restore.description.."</span>"
	end
end

return m
